package com.leon.androidcoursedesign.utils.dataBaseUtils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.lang.reflect.Field;

public class SQLiteHelper extends SQLiteOpenHelper {
    private static final String AccountInfo = "CREATE TABLE \"AccountInfo\" (\n" +
            "\"id\"  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n" +
            "\"uid\"  TEXT NOT NULL UNIQUE,\n" +
            "\"pwd\"  TEXT,\n" +
            "\"phone\"  TEXT NOT NULL UNIQUE\n" +
            ");";

    private static final String Course = "CREATE TABLE \"Course\" (\n" +
            "\"id\"  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n" +
            "\"operationId\"  TEXT NOT NULL,\n" +
            "\"itemId\"  TEXT NOT NULL,\n" +
            "\"courseName\"  TEXT NOT NULL,\n" +
            "\"teacher\"  TEXT,\n" +
            "\"location\"  TEXT,\n" +
            "\"period\"  INTEGER,\n" +
            "\"credit\"  DOUBLE,\n" +
            "\"isDelete\"  BOOLEAN DEFAULT false\n" +
            ");";

    private static final String Notes = "CREATE TABLE \"Note\" (\n" +
            "\"id\"  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n" +
            "\"operationId\"  TEXT NOT NULL,\n" +
            "\"itemId\"  TEXT NOT NULL,\n" +
            "\"noteId\"  TEXT NOT NULL UNIQUE,\n" +
            "\"showName\"  TEXT NOT NULL,\n" +
            "\"saveTime\"  LONG NOT NULL,\n" +
            "\"isDelete\"  BOOLEAN DEFAULT false\n" +
            ");";

    public SQLiteHelper(@Nullable Context context, int version) {
        super(context, "androidCourseDesign.db", null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        //创建数据库表格，通过反射获取SQL语句
        Class<SQLiteHelper> sqLiteHelperClass = SQLiteHelper.class;
        Field[] fields = sqLiteHelperClass.getDeclaredFields();
        for (Field field : fields) {
            try {
                Object sql = field.get(field);

                sqLiteDatabase.execSQL(sql.toString());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

